﻿// CubeRobotBusNode.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "Common.h"
#include "Node.h"
#include "json.hpp"
#include "Package.h"
#include <fstream>

using namespace wsbus;

std::string readFile2(const std::string &fileName)
{
	std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);

	std::ifstream::pos_type fileSize = ifs.tellg();
	ifs.seekg(0, std::ios::beg);

	std::string result;
	result.resize(fileSize);
	ifs.read(&result[0], fileSize);
	return result;
}

int main(int argc, char *argv[])
{
#if 1
	initLog("server1", argv[0], true);
	Node client(2, "127.0.0.1", std::to_string(getBusPort()));
	client.setAuth("admin", "admin");
	client.addRequestHandler(TYPE_NAME(ProtoBase::EchoReq), [](const PackagePtr &msg) -> MoreMessagePtr {
		if (msg->header().bodyType == PacHeader::eJson) {
			std::string command, data;
			if (msg->getMessage(command, data)) {
				auto j = nlohmann::json::parse(data);
				std::string echoText = j["text"].get<std::string>();
				nlohmann::json j2;
				j2["text"] = std::string("[json]") + echoText;
				return Package::newMoreMessage(msg->command(), j2.dump());
			}
		}
		else if (msg->header().bodyType == PacHeader::eProtobuf) {
			auto body = msg->getMessage();
			auto echoReq = static_cast<ProtoBase::EchoReq*>(body.get());
			auto echoRsp = std::make_shared<ProtoBase::EchoRsp>();
			echoRsp->set_text(echoReq->text());
			return Package::newMoreMessage(echoRsp);
		}
		return nullptr;
	});
	client.addPublishHandler(TYPE_NAME(ProtoBase::NodeInfoPush), [](const PackagePtr &msg) {
		auto nodeInfoMsg = msg->getMessage();
		auto nodeInfo = static_cast<ProtoBase::NodeInfoPush*>(nodeInfoMsg.get());
		LOG(INFO) << "=========================";
		for (int i = 0; i < nodeInfo->infolist_size(); i++) {
			const auto &info = nodeInfo->infolist(i);
			LOG(INFO) << "appid:" << info.appid() << ", sessionid:" << info.sessionid();
		}
	});

	client.start();
#else
	initLog("client1", argv[0], true);
	wsbus::Node client(3, "127.0.0.1", std::to_string(getBusPort()));
	client.setAuth("admin", "admin");
	client.start();
	sleepSecond(1);

	int j = 0;
	int errCount = 0;
	const int total = 1;
	for (int i = 0; i < total; i++) {
		std::cout << "send message i:" << i << std::endl;
		auto msg = std::make_shared<ProtoBase::EchoReq>();
		//std::string filestr = readFile2(pathNormalize({ runDir(), "robot.log" }));
		std::string filestr = std::to_string(i);
		int reqSize = filestr.size();
		int rspSize = 0;
		msg->set_text(filestr);

		client.postMessage(client.newRequestPackage(msg, 2), [&j, &errCount, &total](int errcode, const PackagePtr& rspPackage) {
			auto msg = rspPackage->getMessage();
			auto rspMsg = static_cast<ProtoBase::EchoRsp*>(msg.get());
			std::string text = rspMsg->text();
			std::ofstream ofs(pathNormalize({ runDir(), "robot2.log" }), std::ios_base::binary);
			ofs << text;
			if (std::to_string(j) != text) {
				errCount++;
			}
			j++;
			if (j == total - 1) {
				LOG(INFO) << "finished, errcount:" << errCount;
			}
		});
	}
#endif

	while (1) {
		sleepSecond(10);
	}
	return 0;
}

